<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
$config = array(
		//
		'cname' => 'item',
		
		//
		'class_file' => 'module/member/member.class.php',
		'class_name' => 'MemberBase',
		'class_param' => array('_table_status_usable' => false),
		
		//
		'tpl_type' => 'admin',
		'tpl_file' => 'pagelayout/pagelayout_admin_member_status.tpl',
		
		//
		'validate_file' => '',
		'validate_class' => 'CommonValidate',
		'validate_fields' => array('field' => 'name', 'valid' => 'null'),
			);

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'control/item.control.class.php');
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'control/item.content.control.class.php');

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/member/member.status.class.php');

//
class _ControlItemContentBase extends ControlItemContentBase {
	
	//constructor
	function _ControlItemContentBase($config = '') {
		parent::ControlItemContentBase($config);
		
		//
		if( $this->feObj->action == 'updatestatus' ) {
			$this->actionHandler = 'doUpdateStatusActionHandler';
		} elseif( $this->feObj->action == 'live_additem' ) {
			$this->actionHandler = 'doLiveAddItemActionHandler';
		} elseif( $this->feObj->action == 'live_updateitem' ) {
			$this->actionHandler = 'doLiveUpdateItemActionHandler';
		} elseif( $this->feObj->action == 'live_deleteitem' ) {
			$this->actionHandler = 'doLiveDeleteItemActionHandler';
		} elseif( $this->feObj->action == 'school_additem' ) {
			$this->actionHandler = 'doSchoolAddItemActionHandler';
		} elseif( $this->feObj->action == 'school_updateitem' ) {
			$this->actionHandler = 'doSchoolUpdateItemActionHandler';
		} elseif( $this->feObj->action == 'school_deleteitem' ) {
			$this->actionHandler = 'doSchoolDeleteItemActionHandler';
		} elseif( $this->feObj->action == 'service_additem' ) {
			$this->actionHandler = 'doServiceAddItemActionHandler';
		} elseif( $this->feObj->action == 'service_updateitem' ) {
			$this->actionHandler = 'doServiceUpdateItemActionHandler';
		} elseif( $this->feObj->action == 'service_deleteitem' ) {
			$this->actionHandler = 'doServiceDeleteItemActionHandler';
		} elseif( $this->feObj->action == 'deletenotify' ) {
			$this->actionHandler = 'doDeleteNotifyActionHandler';
		}
		//
		if( $this->feObj->operation == 'status' && $this->feObj->_request['id'] > 0 ) {
			$this->displayHandler = 'doEditStatusDisplayHandler';
		} elseif( $this->feObj->operation == 'live' && $this->feObj->_request['id'] > 0 ) {
			$this->displayHandler = 'doStatusLiveDisplayHandler';
		} elseif( $this->feObj->operation == 'school' && $this->feObj->_request['id'] > 0 ) {
			$this->displayHandler = 'doStatusSchoolDisplayHandler';
		} elseif( $this->feObj->operation == 'service' && $this->feObj->_request['id'] > 0 ) {
			$this->displayHandler = 'doStatusServiceDisplayHandler';
		} elseif( $this->feObj->operation == 'notify' ) {
			$this->displayHandler = 'doStatusNotifyDisplayHandler';
		}
	}
	
	//methods
	function doActionHandler() {
		echo 'doActionHandler';
		exit;
	}
	
	function doUpdateStatusActionHandler($obj) {
		$msObj = new MemberStatusBase();
		$msObj->setOwner($this->feObj->_request['id']);
		$msObj->setType($this->feObj->_post['type']);
		//
		$vObj = new CommonValidate();
		$vObj->checkNull($_POST, array('name'));
		if( $vObj->_valid() ) {
			foreach($_POST as $k => $v) {
				if( substr($k, 0, strlen('field_')) == 'field_' ) {
					$data_array[$k] = $v;
				}
			}
			$msObj->updateItem($data_array);
			//
			_dms(':member:success');
			//
			$this->_redirect('', '', 's');
		} else {
			$GLOBALS['_SUBMITTED_'] = 1;
			
			$vm = $vObj->_result(':error');
			$injectObj->addb('func_get_tpl_pagelayout_content', 'content_wrap', 'vm', $vm);
		}
		
		//_dms(':admin:success');
		//$this->_redirect('', array('op'), 's');
	}
	
	//LIVE
	function doLiveAddItemActionHandler($obj) {
		$msObj = new MemberStatusLiveItemBase();
		$msObj->setOwner($this->feObj->_request['id']);
		//
		$vObj = new CommonValidate();
		$vObj->checkNull($_POST, array('name'));
		if( $vObj->_valid() ) {
			foreach($_POST as $k => $v) {
				if( substr($k, 0, strlen('field_')) == 'field_' ) {
					$data_array[$k] = $v;
				}
			}
			$msObj->addItem($data_array);
			//
			_dms(':member:success');
			//
			$this->_redirect('', array('op', 'act', 'op' => 'status', 'type' => 'live'), 's');
		} else {
			$GLOBALS['_SUBMITTED_'] = 1;
			
			$vm = $vObj->_result(':error');
			$injectObj->addb('func_get_tpl_pagelayout_content', 'content_wrap', 'vm', $vm);
		}
	}
	
	function doLiveUpdateItemActionHandler($obj) {
		$msObj = new MemberStatusLiveItemBase();
		$msObj->setOwner($this->feObj->_request['id']);
		//
		$vObj = new CommonValidate();
		$vObj->checkNull($_POST, array('name'));
		if( $vObj->_valid() ) {
			foreach($_POST as $k => $v) {
				if( substr($k, 0, strlen('field_')) == 'field_' ) {
					$data_array[$k] = $v;
				}
			}
			$msObj->updateItem($this->feObj->_request['sid'], $data_array);
			//
			_dms(':member:success');
			//
			$this->_redirect('', array('op', 'act', 'op' => 'status', 'type' => 'live'), 's');
		} else {
			$GLOBALS['_SUBMITTED_'] = 1;
			
			$vm = $vObj->_result(':error');
			$injectObj->addb('func_get_tpl_pagelayout_content', 'content_wrap', 'vm', $vm);
		}
	}
	
	function doLiveDeleteItemActionHandler($obj) {
		$msObj = new MemberStatusLiveItemBase();
		$msObj->setOwner($this->feObj->_request['id']);
		$msObj->deleteItem($this->feObj->_request['sid']);
		
		//
		_dms(':member:success');
		//
		$this->_redirect('', array('op', 'act', 'sid', 'op' => 'status', 'type' => 'live'), 's');
	}
	
	//SCHOOL
	function doSchoolAddItemActionHandler($obj) {
		$msObj = new MemberStatusSchoolItemBase();
		$msObj->setOwner($this->feObj->_request['id']);
		//
		$vObj = new CommonValidate();
		$vObj->checkNull($_POST, array('name'));
		if( $vObj->_valid() ) {
			foreach($_POST as $k => $v) {
				if( substr($k, 0, strlen('field_')) == 'field_' ) {
					$data_array[$k] = $v;
				}
			}
			$msObj->addItem($data_array);
			//
			_dms(':member:success');
			//
			$this->_redirect('', array('op', 'act', 'op' => 'status', 'type' => 'school'), 's');
		} else {
			$GLOBALS['_SUBMITTED_'] = 1;
			
			$vm = $vObj->_result(':error');
			$injectObj->addb('func_get_tpl_pagelayout_content', 'content_wrap', 'vm', $vm);
		}
	}
	
	function doSchoolUpdateItemActionHandler($obj) {
		$msObj = new MemberStatusSchoolItemBase();
		$msObj->setOwner($this->feObj->_request['id']);
		//
		$vObj = new CommonValidate();
		$vObj->checkNull($_POST, array('name'));
		if( $vObj->_valid() ) {
			foreach($_POST as $k => $v) {
				if( substr($k, 0, strlen('field_')) == 'field_' ) {
					$data_array[$k] = $v;
				}
			}
			$msObj->updateItem($this->feObj->_request['sid'], $data_array);
			//
			_dms(':member:success');
			//
			$this->_redirect('', array('op', 'act', 'op' => 'status', 'type' => 'school'), 's');
		} else {
			$GLOBALS['_SUBMITTED_'] = 1;
			
			$vm = $vObj->_result(':error');
			$injectObj->addb('func_get_tpl_pagelayout_content', 'content_wrap', 'vm', $vm);
		}
	}
	
	function doSchoolDeleteItemActionHandler($obj) {
		$msObj = new MemberStatusSchoolItemBase();
		$msObj->setOwner($this->feObj->_request['id']);
		$msObj->deleteItem($this->feObj->_request['sid']);
		
		//
		_dms(':member:success');
		//
		$this->_redirect('', array('op', 'act', 'sid', 'op' => 'status', 'type' => 'school'), 's');
	}
	
	//SERVICE
	function doServiceAddItemActionHandler($obj) {
		$msObj = new MemberStatusServiceBase();
		$msObj->setOwner($this->feObj->_request['id']);
		//
		$vObj = new CommonValidate();
		$vObj->checkNull($_POST, array('name'));
		if( $vObj->_valid() ) {
			foreach($_POST as $k => $v) {
				if( substr($k, 0, strlen('field_')) == 'field_' ) {
					$data_array[$k] = $v;
				}
			}
			$msObj->addItem($data_array);
			//
			_dms(':member:success');
			//
			$this->_redirect('', array('op', 'act', 'op' => 'status', 'type' => 'service'), 's');
		} else {
			$GLOBALS['_SUBMITTED_'] = 1;
			
			$vm = $vObj->_result(':error');
			$injectObj->addb('func_get_tpl_pagelayout_content', 'content_wrap', 'vm', $vm);
		}
	}
	
	function doServiceUpdateItemActionHandler($obj) {
		$msObj = new MemberStatusServiceBase();
		$msObj->setOwner($this->feObj->_request['id']);
		//
		$vObj = new CommonValidate();
		$vObj->checkNull($_POST, array('name'));
		if( $vObj->_valid() ) {
			foreach($_POST as $k => $v) {
				if( substr($k, 0, strlen('field_')) == 'field_' ) {
					$data_array[$k] = $v;
				}
			}
			$msObj->updateItem($this->feObj->_request['sid'], $data_array);
			//
			_dms(':member:success');
			//
			$this->_redirect('', array('op', 'act', 'op' => 'status', 'type' => 'service'), 's');
		} else {
			$GLOBALS['_SUBMITTED_'] = 1;
			
			$vm = $vObj->_result(':error');
			$injectObj->addb('func_get_tpl_pagelayout_content', 'content_wrap', 'vm', $vm);
		}
	}
	
	function doServiceDeleteItemActionHandler($obj) {
		$msObj = new MemberStatusServiceBase();
		$msObj->setOwner($this->feObj->_request['id']);
		$msObj->deleteItem($this->feObj->_request['sid']);
		
		//
		_dms(':member:success');
		//
		$this->_redirect('', array('op', 'act', 'sid', 'op' => 'status', 'type' => 'service'), 's');
	}
	
	//NOTIFY
	function doDeleteNotifyActionHandler($obj) {
		$msObj = new MemberStatusBase();
		
		$ids = util_get_param($_POST, 'cb');
		if( is_array($ids) ) {
			$msObj->notifyObj->deleteItems($ids);
			//
			$this->_redirect('', '', 's');
		}
		
		//
		_dms(':admin:success');
		$this->_redirect('', array('op'), 's');
	}
	
	//
	function doDisplayHandler() {
		echo 'doDisplayHandler';
		exit;
	}
	
	function doEditStatusDisplayHandler($obj) {
		$member = $this->modelObj->getItem($this->feObj->_request['id'], 'id');
		if( $member['id'] == 0 ) {
			//
			_dms(':member:no_exist');
			//
			$mainpart = 'no_exist';
		} else {
			$this->feObj->addContent('member', $member);
			//
			$msObj = new MemberStatusBase();
			$msObj->setOwner($this->feObj->_request['id']);
			$msObj->setType($this->feObj->_request['type']);
			$item = $msObj->getItem();
			$items = $msObj->getItems();
			$this->feObj->addContent('item', $item);
			$this->feObj->addContent('items', $items);
			
			//
			if( $this->feObj->_request['type'] == '' ) {
				$mainpart = 'edit';
			} else {
				$mainpart = 'edit_' . $this->feObj->_request['type'];
			}
		}
		//
		$this->feObj->content_wrap['mainpart'] = $mainpart;
	}
	
	function doStatusLiveDisplayHandler($obj) {
		$member = $this->modelObj->getItem($this->feObj->_request['id'], 'id');
		if( $member['id'] == 0 ) {
			//
			_dms(':member:no_exist');
			//
			$mainpart = 'no_exist';
		} else {
			$this->feObj->addContent('member', $member);
			//
			$msObj = new MemberStatusBase();
			$msObj->setOwner($this->feObj->_request['id']);
			$msObj->setType($this->feObj->_request['type']);
			$item = $msObj->getItem();
			$items = $msObj->getItems();
			$this->feObj->addContent('item', $item);
			$this->feObj->addContent('items', $items);
			
			//
			if( $this->feObj->_request['act'] == 'add' ) {
				$mainpart = 'edit_live_additem';
			} elseif( $this->feObj->_request['act'] == 'edit' ) {
				$mainpart = 'edit_live_edititem';
			}
		}
		//
		$this->feObj->content_wrap['mainpart'] = $mainpart;
	}

	function doStatusSchoolDisplayHandler($obj) {
		$member = $this->modelObj->getItem($this->feObj->_request['id'], 'id');
		if( $member['id'] == 0 ) {
			//
			_dms(':member:no_exist');
			//
			$mainpart = 'no_exist';
		} else {
			$this->feObj->addContent('member', $member);
			//
			$msObj = new MemberStatusBase();
			$msObj->setOwner($this->feObj->_request['id']);
			$msObj->setType($this->feObj->_request['type']);
			$item = $msObj->getItem();
			$items = $msObj->getItems();
			$this->feObj->addContent('item', $item);
			$this->feObj->addContent('items', $items);
			
			//
			if( $this->feObj->_request['act'] == 'add' ) {
				$mainpart = 'edit_school_additem';
			} elseif( $this->feObj->_request['act'] == 'edit' ) {
				$mainpart = 'edit_school_edititem';
			}
		}
		//
		$this->feObj->content_wrap['mainpart'] = $mainpart;
	}

	function doStatusServiceDisplayHandler($obj) {
		$member = $this->modelObj->getItem($this->feObj->_request['id'], 'id');
		if( $member['id'] == 0 ) {
			//
			_dms(':member:no_exist');
			//
			$mainpart = 'no_exist';
		} else {
			$this->feObj->addContent('member', $member);
			//
			$msObj = new MemberStatusBase();
			$msObj->setOwner($this->feObj->_request['id']);
			$msObj->setType($this->feObj->_request['type']);
			$item = $msObj->getItem();
			$items = $msObj->getItems();
			$this->feObj->addContent('item', $item);
			$this->feObj->addContent('items', $items);
			
			//
			if( $this->feObj->_request['act'] == 'add' ) {
				$mainpart = 'edit_service_additem';
			} elseif( $this->feObj->_request['act'] == 'edit' ) {
				$mainpart = 'edit_service_edititem';
			}
		}
		//
		$this->feObj->content_wrap['mainpart'] = $mainpart;
	}
	
	function doStatusNotifyDisplayHandler($obj) {
		$msObj = new MemberStatusBase();
		
		//
		$filter = $this->_get_items_filter();
		$limit_length = $this->feObj->limit_length;
		$limit_start = ($this->feObj->limit_page - 1) * $limit_length;
		$limit_total = $msObj->notifyObj->getItemsTotal($filter, -1);
		//
		$msObj->notifyObj->setQueryLimitStart($limit_start);
		$msObj->notifyObj->setQueryLimitLength($limit_length);
		$items = $msObj->notifyObj->getItems($filter, $check, $limit);
		//
		for($i=0, $n=sizeof($items); $i<$n; $i++) {
			$items[$i] = $msObj->notifyObj->formatItem($items[$i]);
		}
		//
		$parameters = util_get_all_get_params(array('page'));
		$link_array = array(
					'page' => '',
					'parameters' => $parameters,
						);
		$navigation = util_common_navigation($link_array, $limit_total, $limit_start, $limit_length);
		
		//
		$this->feObj->content_wrap['mainpart'] = 'notify';
		$this->feObj->content_wrap['items'] = $items;
		$this->feObj->content_wrap['navigation'] = $navigation;
	}
	
	//
	function _doAfter() {
		
	}
}

//
$cntlObj = new _ControlItemContentBase($config);
$cntlObj->process();
//
?>